package com.example.yam.greenapp;

import android.app.Application;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.util.LruCache;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Created by 杨建利 on 2017/1/25.
 */

public class MyApplication extends Application {
    public static RequestQueue httpQuque;//volly框架的请求队列
    public static ImageLoader  mImageLoader; //创建ImageLoader,用于将图片存入缓存和从缓存中取出图片

    //indexFragment
    public static List<String> piclist=new ArrayList<String>();//轮播图片的url列表
    public static List<Classfy> burl = new ArrayList<Classfy>();//4个按钮的分类信息
    //网站域名
    public static final String localhost="http://10.0.3.2/";
    //个人信息
    public static User user;

    //临时购物车--dialog
    public static List<DGoods> dialoggoodsList = new ArrayList<DGoods>();
    public static float diaSum = 0;//临时购物车的总价

    //当前选中的收货地址
    public static Shdz myshdz;

    @Override
    public void onCreate() {
        super.onCreate();
        httpQuque = Volley.newRequestQueue(getApplicationContext());
        mImageLoader=new ImageLoader(httpQuque,new BitmapCache());
    }
    public static ImageLoader getmImageLoader() {
        return mImageLoader;
    }
    public static RequestQueue getHttpQueue(){
        return httpQuque;
    }
    //缓存机制
    public class BitmapCache implements ImageLoader.ImageCache {
        private LruCache<String, Bitmap> mCache;
        public BitmapCache(){
            // 获取到可用内存的最大值，使用内存超出这个值会引起OutOfMemory异常
            // 使用最大可用内存值的1/8作为缓存的大小。
            int maxsize=(int)(Runtime.getRuntime().maxMemory()/1024)/8;
            mCache=new LruCache<String, Bitmap>(maxsize){
                // 重写此方法来衡量每张图片的大小，默认返回图片数量
                @Override
                protected int sizeOf(String key, Bitmap value) {
                    return super.sizeOf(key, value);
                }
            };
        }
        /**
         * 获取缓存中图片
         */
        @Override
        public Bitmap getBitmap(String key) {
            return mCache.get(key);
        }
        /**
         * 存入缓存
         */
        @Override
        public void putBitmap(String key, Bitmap bitmap) {
            if(bitmap!=null)mCache.put(key, bitmap);
        }
    }
    public static void setRollViewUrl(String ss){
        piclist.add(ss);
    }
    public static  List<String> getPiclist(){
        return piclist;
    }

    //将购物车数据转化为jsonArray形式的字符串
    public static String changeDiagoodListToJSONArrayString(){
        String ss=null;
        JSONArray jsonArray = new JSONArray();
        for(int i=0,len=dialoggoodsList.size();i<len;i++){
            JSONObject jsonObject = new JSONObject();
            DGoods dg = dialoggoodsList.get(i);
            try {
                jsonObject.put("gid",dg.getId());
                jsonObject.put("num",dg.getNum());
            }catch (JSONException e) {
                e.printStackTrace();
            }
            jsonArray.put(jsonObject);
        }
        ss = jsonArray.toString();
        return ss;
    }
    //将收货信息转化为jsonobject形式
    public static String changeReceiverMessageToJSONObjectString() {
        String ss = null;
        JSONObject jb = new JSONObject();

        try {
            jb.put("receiver",URLEncoder.encode(myshdz.getName(),"utf-8"));
            jb.put("phone",myshdz.getPhone());
            jb.put("location",URLEncoder.encode(myshdz.getLocation(), "utf-8"));
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        ss = jb.toString();
        return ss;
    }
}
